/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ipfms.workflow;

import com.google.inject.Inject;
import ipfms.dao.BuildingDAO;
import ipfms.model.Building;
import ipfms.model.Inbox;
import ipfms.model.PenggunaPeranan;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.log4j.Logger;

/**
 *
 * @author fikri
 */
public class WorkflowService {
    
    private static ProcessEngine processEngine;
    private RepositoryService repositoryService;
    private RuntimeService runtimeService;
    
    @Inject BuildingDAO buildingDAO;
       
    
    private static final Logger LOG = Logger.getLogger( WorkflowService.class );
    
    static {
        LOG.info(" initiate Workflow Process Engine..");
        processEngine = ProcessEngines.getDefaultProcessEngine();        
    }
    
    
    public String initiateWorkflow( String workflowId , Map<String, Object> variables) {
        runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey( workflowId,variables );  
        
        LOG.info("workflow initiated = " + processInstance.getProcessInstanceId());
        
        Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).singleResult();
        
        
//        return task.getId();
        return processInstance.getProcessInstanceId();
    }
    
    public void acquireTask( String taskId, String userId ) {
        
        Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(taskId).singleResult();
        
         processEngine.getTaskService().claim( task.getId(), userId );
    }
    
    public Task getTask( String taskId ) {
        return processEngine.getTaskService().createTaskQuery().processInstanceId(taskId).singleResult();
    }
    
    public List<Inbox> queryTasks(String userId, List<PenggunaPeranan> roles) {        
        //fixme!! proper searching ?
        List<Task> tasks = new ArrayList<Task>();
        List<Inbox> inboxs = new ArrayList<Inbox>();
        
        for (PenggunaPeranan role : roles) {
            List<Task> t = processEngine.getTaskService().createTaskQuery()
                    .taskCandidateGroup(role.getPeranan().getDesc()).list();
            for (Task task : t) {
                tasks.add(task);
            }
        }
        
        List<Task> t = processEngine.getTaskService().createTaskQuery().taskAssignee(userId).list();
        for (Task task : t) tasks.add(task);
        
        
        for (Task task : tasks) {
             Map<String,Object> maps = processEngine.getTaskService().getVariables(task.getId());
                if ( maps != null ){
                    String idBuilding = (String)maps.get("idBuilding");
                    Building building = buildingDAO.findById(Integer.parseInt(idBuilding));
                    if (building != null) {
                        Inbox inbox = new Inbox();
                        inbox.setBuilding(building);
                        inbox.setWorkflowId(task.getProcessInstanceId());
                        inboxs.add(inbox);
                    }
                }
        }               
        
        return inboxs;        
    }
    
    public void complete( String taskId ) {
        Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(taskId).singleResult();
        
        processEngine.getTaskService().complete(task.getId());
    }
    
    
    public static void  main (String... args) {
        
        List<Task> tasks = processEngine.getTaskService().createTaskQuery().taskCandidateGroup("accountancy").list();
        System.out.println("tasks" + tasks.size());
    }
   
}
